package com.tarena.lbs.marketing.web.repository.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.marketing.converter.MarketingActivityConverter;
import com.tarena.lbs.marketing.web.mapper.MarketingActivityMapper;
import com.tarena.lbs.marketing.web.repository.MarketingActivityRepository;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.base.protocol.pager.BasePageList;

import com.tarena.lbs.common.attach.enums.BusinessTypeEnum;
import com.tarena.lbs.common.marketing.enums.MarketingResultEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.attach.param.AttachUpdateParam;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.marketing.dos.MarketingActivityDO;
import com.tarena.lbs.pojo.marketing.param.MarketingActivityParam;
import com.tarena.lbs.pojo.marketing.param.UpdateParam;
import com.tarena.lbs.pojo.marketing.query.MarketingActivityQuery;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;



@Service
public class MarketingActivityRepositoryImpl implements MarketingActivityRepository {

    @Resource
    private MarketingActivityConverter marketingActivityConverter;

    @Resource
    private MarketingActivityMapper marketingActivityMapper;

    @Resource
    private AttachApi attachApi;

    @Override
    public void updateStatus(UpdateParam updateParam) {
        MarketingActivityDO tempDO = new MarketingActivityDO();
        tempDO.setId(updateParam.getId());
        tempDO.setStatus(updateParam.getStatus());
        tempDO.setUpdateAt(new Date());
        marketingActivityMapper.updateByPrimaryKeySelective(tempDO);
    }

    @Override
    public void insert(MarketingActivityParam param) throws com.tarena.lbs.base.protocol.exception.BusinessException {
        MarketingActivityDO tempDO = marketingActivityConverter.param2DO(param);
        marketingActivityMapper.insert(tempDO);
        //更新附件
        List<String> fileIds = new ArrayList<>(Arrays.asList(param.getImgIds()));
        createAttach(tempDO.getId(), BusinessTypeEnum.ACTIVITY.getType(),fileIds);
    }

    private void createAttach(Integer businessId,Integer businessType,List<String> fileIds) {
        List<AttachUpdateParam> attachUpdateParams = new ArrayList<>();
        fileIds.forEach(id -> {
            AttachUpdateParam updateParam = new AttachUpdateParam();
            updateParam.setId(Integer.valueOf(id));
            updateParam.setBusinessId(businessId);
            updateParam.setBusinessType(businessType);
            updateParam.setIsCover(0);
            attachUpdateParams.add(updateParam);
        });
        attachApi.batchUpdateAttachByIdList(attachUpdateParams);
    }

    @Override
    public void update(MarketingActivityParam param) throws BusinessException {
        MarketingActivityDO tempDO = new MarketingActivityDO();
        BeanUtils.copyProperties(param,tempDO);
        tempDO.setUpdateAt(new Date());
        try {
            tempDO.setStartDate(DateUtils.parseDate(param.getStartDate(),"yyyy-MM-dd HH:mm:ss"));
            tempDO.setEndDate(DateUtils.parseDate(param.getEndDate(),"yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            throw new BusinessException(MarketingResultEnum.DATE_ERROR);
        }
        marketingActivityMapper.updateByPrimaryKeySelective(tempDO);
    }

    @Override
    public BasePageList<MarketingActivityBO> list(MarketingActivityQuery query) {
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            query.setBusinessId(loginUser.getId());
        }
        int page = query.getPageNo();
        int pageSize = query.getPageSize();
        //new PageQuery(page, pageSize);
        List<MarketingActivityDO> list = marketingActivityMapper.selectList(query);
        PageInfo<MarketingActivityDO> pageInfo = new PageInfo<>(list);
        List<MarketingActivityBO> activityBOList = pageInfo.getList().stream().map(activityDO -> {
            return marketingActivityConverter.do2bo(activityDO);
        }).collect(Collectors.toList());
        return new BasePageList<MarketingActivityBO>(activityBOList,pageInfo.getTotal());
    }

    @Override
    public List<MarketingActivityBO> selectListByStatus() {
        List<MarketingActivityDO> list = marketingActivityMapper.selectListByStatus();
        return marketingActivityConverter.dos2bos(list);
    }

    @Override
    public MarketingActivityBO getDetail(Integer activityId) {
        MarketingActivityDO marketingActivityDO = marketingActivityMapper.selectByPrimaryKey(activityId);
        return marketingActivityConverter.do2bo(marketingActivityDO);
    }

    @Override
    public void batchUpdateActivityStatus(List<UpdateParam> params) {
        marketingActivityMapper.batchUpdateActivityStatus(params);
    }

    @Override
    public MarketingActivityBO getActivityByStoreId(Integer storeId) {
        MarketingActivityDO marketingActivityDO = marketingActivityMapper.getActivityByStoreId(storeId);
        return marketingActivityConverter.do2bo(marketingActivityDO);
    }

    @Override
    public MarketingActivityBO getActivityById(Integer id) {
        MarketingActivityDO marketingActivityDO = marketingActivityMapper.selectByPrimaryKey(id);
        return marketingActivityConverter.do2bo(marketingActivityDO);
    }

}
